﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using ITProject.Domain.Entities;
using System.IO;

namespace ITProject.WebUI.Models.ProjectAdmin
{
    public class ProjectAdminEditProjectViewModel
    {
        [Required(ErrorMessage = "Musisz podać opis projektu")]
        [Display(Name = "Opis projektu")]
        [MinLength(20, ErrorMessage = "Opis projektu powinien zawierać co najmniej 20 znaków")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required(ErrorMessage = "Musisz podać skrócony opis projektu")]
        [Display(Name = "Skrócony opis projektu")]
        [MinLength(8, ErrorMessage = "Skrócony opis projektu powinien zawierać co najmniej 8 znaków")]
        [MaxLength(80, ErrorMessage = "Skrócony opis projektu powinien zawierać co najwyżej 80 znaków")]
        [DataType(DataType.MultilineText)]
        public string ShortDescription { get; set; }

        [Display(Name = "Logo projektu")]
        public HttpPostedFileBase Logo { get; set; }

        // atrybut ukryty
        public int ProjectId { get; set; }
        public string Name { get; set; }

        public string LogoName { get; set; }
        public bool DeleteLogo { get; set; }

        public static ProjectAdminEditProjectViewModel Convert(ITProject.Domain.Entities.Project project)
        {
            ProjectAdminEditProjectViewModel vm = new ProjectAdminEditProjectViewModel();
            vm.Description = project.Description;
            vm.ShortDescription = project.ShortDescription;
            vm.ProjectId = project.Id;
            vm.Name = project.Name;
            vm.LogoName = project.LogoPath;
            vm.DeleteLogo = false;
            return vm;
        }

        public static ITProject.Domain.Entities.Project Convert(ProjectAdminEditProjectViewModel vm, string fileName)
        {
            ITProject.Domain.Entities.Project p = new ITProject.Domain.Entities.Project();
            p.Description = vm.Description;
            p.LogoPath = vm.Logo==null?null:fileName;
            p.ShortDescription = vm.ShortDescription;
            p.Id = vm.ProjectId;
            return p;
        }
    }
}